#include "stdafx.h"
#include "BBViewer.h"
#include "MainFrm.h"
#include "UIMain.h"

void CUIMain::GetImageData(LPVOID pImageData)
{
	if (!m_bLoadUserData)
		return;
	LPST_IMAGE_DATA pData = (LPST_IMAGE_DATA) pImageData;

	m_WndTimeBar.SetPlayInfo((int)pData->dwIFrameCount);
}

void CUIMain::GetAudioData(LPVOID pAudioData)
{
	if (!m_bLoadUserData)
		return;
	LPST_AUDIO_DATA pData = (LPST_AUDIO_DATA) pAudioData;
}

void CUIMain::GetUserData(LPVOID pUserData)
{
	if (!m_bLoadUserData)
		return;

	LPST_USER_DATA pData = (LPST_USER_DATA)pUserData;

	PPICTURE_HEADER_EX pUserStreamData = (PPICTURE_HEADER_EX)pData->pUserData;

	static FLOAT fSpeed = 0;
	static time_t tPlayTime = 0;
	static LONGLONG llGSenUpdate = 0;

	if (pData->dwChannel != 0)
		return;

	if (m_stImageInfo.stFileInfo.nCurIndex != pData->nCurIndex)
	{
		m_WndGps.SetPlayBlockTime((DWORD)pData->tStartTime, (DWORD)pData->tEndTime);
		m_WndTimeBar.SetPlayIFrameInfo(pData->nTotalIFrameCount);

		if (pData->nSearchType == search_time)
		{
			m_WndListTime.SetRecIndex(pData->nCurIndex);
		}
		else
		{
			m_WndListEvent.SetRecIndex(pData->nCurIndex);
		}

	}


	//////////////////////////////////////////////////////////////////////////
	//set speed
	if (fSpeed != pUserStreamData->gps_speed)
	{
		fSpeed = pUserStreamData->gps_speed;
		m_WndSpeedmeter.SetSpeed(fSpeed);
	}

	//////////////////////////////////////////////////////////////////////////
	//GPS
	if (tPlayTime != pData->tPlayTime)
	{
		tPlayTime = pData->tPlayTime;

		m_WndGps.SetGpsInfo((DWORD)tPlayTime, pUserStreamData->gps_latitude/100, pUserStreamData->gps_longitude/100, pUserStreamData->gps_speed, m_pStatusMgr->GetSpeedometer());
	}

	//////////////////////////////////////////////////////////////////////////
	//G-Sensor
	LONGLONG llTime, llGap;
	int nDir = 0;
	llTime = pData->tPlayTime;
	llTime = llTime*1000+pData->dwPlayTimeMillisec;
	llGap = llTime - llGSenUpdate;
	if (llGap<0)
	{
		nDir = 1;
		llGap = -llGap;
	}

	if (llGap>100)
	{
		llGSenUpdate = llTime;

		m_WndGSensorGraph.SetGSensorVal((int)(pUserStreamData->gsensor_x*1000),
										(int)(pUserStreamData->gsensor_y*1000),
										(int)(pUserStreamData->gsensor_z*1000),
										nDir);
		m_WndGSensor.SetSensorValue(pUserStreamData->gsensor_x,
									pUserStreamData->gsensor_y,
									pUserStreamData->gsensor_z);
	}

	//////////////////////////////////////////////////////////////////////////
	//Map
	if (!m_WrapMap.GetStartState())
		m_WrapMap.StartMap();
	m_WrapMap.SetGps(pUserStreamData->gps_latitude/100, pUserStreamData->gps_longitude/100, pUserStreamData->gps_speed);

	//////////////////////////////////////////////////////////////////////////
	

	//////////////////////////////////////////////////////////////////////////
	//
#ifdef _EX_HEADER_INFO
	TRACE(_T("gyro = x: %4.3f, y: %4.3f, z: %4.3f, %d\n"), 
		pUserStreamData->gyro_x,
		pUserStreamData->gyro_y,
		pUserStreamData->gyro_z,
		sizeof(PICTURE_HEADER_EX));
#endif



	//copy info
	m_stImageInfo.stFileInfo.nTotalIFrameCount = pData->nTotalIFrameCount;
	m_stImageInfo.stFileInfo.nSearchType = pData->nSearchType;
	m_stImageInfo.stFileInfo.tStartTime = pData->tStartTime;
	m_stImageInfo.stFileInfo.nCurIndex = pData->nCurIndex;
	m_stImageInfo.stFileInfo.tEndTime = pData->tEndTime;

	m_stImageInfo.tPlayTime = pUserStreamData->time;
	m_stImageInfo.tPlayTimeMillisec = pUserStreamData->msec;
	m_stImageInfo.gps_longitude = pUserStreamData->gps_longitude;
	m_stImageInfo.gps_latitude = pUserStreamData->gps_latitude;
	m_stImageInfo.gps_speed = pUserStreamData->gps_speed;
	m_stImageInfo.gsensor_x = pUserStreamData->gsensor_x;
	m_stImageInfo.gsensor_y = pUserStreamData->gsensor_y;
	m_stImageInfo.gsensor_z = pUserStreamData->gsensor_z;
}

void CUIMain::GetUserInfo(LPVOID pUserInfo)
{
	if (!m_bLoadUserData)
		return;

	LPST_IMAGE_INFO lpImageInfo = (LPST_IMAGE_INFO)pUserInfo; 

	if (lpImageInfo->dwChannel != 0)
		return;

	if (m_stImageInfo.stFileInfo.nCurIndex != lpImageInfo->stFileInfo.nCurIndex)
	{
		m_WndGps.SetPlayBlockTime((DWORD)lpImageInfo->stFileInfo.tStartTime, (DWORD)lpImageInfo->stFileInfo.tEndTime);
		m_WndTimeBar.SetPlayIFrameInfo(lpImageInfo->stFileInfo.nTotalIFrameCount);

		if (lpImageInfo->stFileInfo.nSearchType == search_time)
		{
			m_WndListTime.SetRecIndex(lpImageInfo->stFileInfo.nCurIndex);
		}
		else
		{
			m_WndListEvent.SetRecIndex(lpImageInfo->stFileInfo.nCurIndex);
		}

		if (lpImageInfo->IsAvailableToUserDataAll &&(m_WndGSensorGraph.GetGSensorGraphType() == GSENSOR_GRAPH_FILE_BASED))
			m_WndGSensorGraph.SetTotalGSensorData(lpImageInfo->pUserDataAll, lpImageInfo->dwUserDataSizeAll, lpImageInfo->dwTotalUserDataCount);
	}

	if (!m_WndGSensorGraph.IsLoaded() && lpImageInfo->IsAvailableToUserDataAll)
	{
		if (lpImageInfo->IsAvailableToUserDataAll)
			m_WndGSensorGraph.SetTotalGSensorData(lpImageInfo->pUserDataAll, lpImageInfo->dwUserDataSizeAll, lpImageInfo->dwTotalUserDataCount);
	}

	static FLOAT fSpeed = 0;
	static time_t tPlayTime = 0;
	static LONGLONG llGSenUpdate = 0;

	//////////////////////////////////////////////////////////////////////////
	//set speed
	if (fSpeed != lpImageInfo->gps_speed)
	{
		fSpeed = lpImageInfo->gps_speed;
		m_WndSpeedmeter.SetSpeed(fSpeed);
	}

	//////////////////////////////////////////////////////////////////////////
	//GPS
	if (tPlayTime != lpImageInfo->tPlayTime)
	{
		tPlayTime = lpImageInfo->tPlayTime;

		m_WndGps.SetGpsInfo((DWORD)tPlayTime, lpImageInfo->gps_latitude/100, lpImageInfo->gps_longitude/100, lpImageInfo->gps_speed, m_pStatusMgr->GetSpeedometer());
	}

	//////////////////////////////////////////////////////////////////////////
	//G-Sensor
	LONGLONG llTime, llGap;
	int nDir = 0;
	llTime = lpImageInfo->tPlayTime;
	llTime = llTime*1000+lpImageInfo->tPlayTimeMillisec;
	llGap = llTime - llGSenUpdate;
	if (llGap<0)
	{
		nDir = 1;
		llGap = -llGap;
	}

	if ((m_WndGSensorGraph.GetGSensorGraphType() == GSENSOR_GRAPH_FILE_BASED) && lpImageInfo->IsAvailableToUserDataAll)
		m_WndGSensorGraph.SetPos(lpImageInfo->nCurUserDataPos);
	if (llGap>100)
	{
		llGSenUpdate = llTime;

		if ((m_WndGSensorGraph.GetGSensorGraphType() == GSENSOR_GRAPH_TIME_BASED) || !lpImageInfo->IsAvailableToUserDataAll)
			m_WndGSensorGraph.SetGSensorVal((int)(lpImageInfo->gsensor_x*1000),
						(int)(lpImageInfo->gsensor_y*1000),
						(int)(lpImageInfo->gsensor_z*1000),
						nDir,
						FALSE);
		m_WndGSensor.SetSensorValue(lpImageInfo->gsensor_x,
			lpImageInfo->gsensor_y,
			lpImageInfo->gsensor_z);

	}

	//////////////////////////////////////////////////////////////////////////
	//Map
	if (!m_WrapMap.GetStartState())
		m_WrapMap.StartMap();
	m_WrapMap.SetGps(lpImageInfo->gps_latitude/100, lpImageInfo->gps_longitude/100, lpImageInfo->gps_speed);

	//////////////////////////////////////////////////////////////////////////
	//time bar
	m_WndTimeBar.SetPlayInfo(lpImageInfo->nIFrameCount);

	m_stImageInfo = *lpImageInfo;
}

void CUIMain::TimeBarLoad(LPST_TIMEBAR_LOAD_DATA lpData)
{
	m_WndTimeBar.LoadTimeBar(lpData);
}
